package moe.eairpeter.sfpapi;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;

import me.mrCookieSlime.Slimefun.SlimefunStartup;
import me.mrCookieSlime.Slimefun.Objects.Research;

/**
 * @author EAirPeter
 */
public class TaskReadAll extends BukkitRunnable {
    
    private boolean processing = false;
    private List<Player> players = null;
    private List<String> ranks = null;
    private List<ResIdLv> researches = null;
    private final Plugin plugin;
    
    TaskReadAll(Plugin plugin_) {
        plugin = plugin_;
    }
    
    @Override
    public void run() {
        if (processing)
            return;
        players = new LinkedList<>(Bukkit.getOnlinePlayers());
        ranks = new ArrayList<>(
            SlimefunStartup.getCfg().getStringList("research-ranks")
        );
        researches = new ArrayList<>();
        for (Research r : Research.list())
            researches.add(new ResIdLv(r.getID(), r.getLevel()));
        processing = true;
        new BukkitRunnable() {
            @Override
            public void run() {
                Map<UUID, PlayerInfo> map = new HashMap<>();
                for (Player p : players) {
                    ConfigurationSection cfgsec = YamlConfiguration.
                        loadConfiguration(new File(
                            "data-storage/Slimefun/Players/" +
                            p.getUniqueId() + ".yml"
                        )).
                        getConfigurationSection("researches");
                    int count = 0;
                    int levels = 0;
                    if (cfgsec != null) {
                        Set<String> researched = cfgsec.getKeys(false);
                        for (ResIdLv ril : researches)
                            if (researched.contains(String.valueOf(ril.id))) {
                                ++count;
                                levels += ril.lv;
                            }
                    }
                    float progress = Math.round(
                        count * 10000.0f / researches.size()
                    ) / 100.0f;
                    int index = Math.round(progress * ranks.size() / 100.0f);
                    String rank = ranks.get(index > 0 ? index - 1 : index);
                    map.put(
                        p.getUniqueId(),
                        new PlayerInfo(levels, rank, progress)
                    );
                }
                PlaceholderHook.refMap.getAndSet(map);
                processing = false;
            }
        }.runTaskAsynchronously(plugin);
    }
    
    private static class ResIdLv {
        
        public final int id;
        public final int lv;
        
        public ResIdLv(int id_, int lv_) {
            id = id_;
            lv = lv_;
        }
        
    }
    
}
